
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _extends2 = _interopRequireDefault(require('@babel/runtime/helpers/extends'));

const _utils = require('../utils');

const _constant = require('../utils/constant');

const _raf = require('../utils/dom/raf');

const _relation = require('../mixins/relation');

const _cell = _interopRequireDefault(require('../cell'));

const _shared = require('../cell/shared');

// Utils
// Mixins
// Components
const _createNamespace = (0, _utils.createNamespace)('collapse-item');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];

const CELL_SLOTS = ['title', 'icon', 'right-icon'];

const _default = createComponent({
  mixins: [(0, _relation.ChildrenMixin)('vanCollapse')],
  props: (0, _extends2.default)({}, _shared.cellProps, {
    name: [Number, String],
    disabled: Boolean,
    isLink: {
      type: Boolean,
      default: true,
    },
  }),
  data: function data() {
    return {
      show: null,
      inited: null,
    };
  },
  computed: {
    currentName: function currentName() {
      return (0, _utils.isDef)(this.name) ? this.name : this.index;
    },
    expanded: function expanded() {
      const _this = this;

      if (!this.parent) {
        return null;
      }

      const _this$parent = this.parent;
      const { value } = _this$parent;
      const { accordion } = _this$parent;

      if (process.env.NODE_ENV !== 'production' && !accordion && !Array.isArray(value)) {
        console.error('[Vant] Collapse: type of prop "value" should be Array');
        return;
      }

      return accordion ? value === this.currentName : value.some((name) => name === _this.currentName);
    },
  },
  created: function created() {
    this.show = this.expanded;
    this.inited = this.expanded;
  },
  watch: {
    expanded: function expanded(_expanded, prev) {
      const _this2 = this;

      if (prev === null) {
        return;
      }

      if (_expanded) {
        this.show = true;
        this.inited = true;
      } // Use raf: flick when opened in safari
      // Use nextTick: closing animation failed when set `user-select: none`


      const nextTick = _expanded ? this.$nextTick : _raf.raf;
      nextTick(() => {
        const _this2$$refs = _this2.$refs;
        const { content } = _this2$$refs;
        const { wrapper } = _this2$$refs;

        if (!content || !wrapper) {
          return;
        }

        const { offsetHeight } = content;

        if (offsetHeight) {
          const contentHeight = `${offsetHeight}px`;
          wrapper.style.height = _expanded ? 0 : contentHeight; // use double raf to ensure animation can start in mobile safari

          (0, _raf.doubleRaf)(() => {
            wrapper.style.height = _expanded ? contentHeight : 0;
          });
        } else {
          _this2.onTransitionEnd();
        }
      });
    },
  },
  methods: {
    onClick: function onClick() {
      if (this.disabled) {
        return;
      }

      const { parent } = this;
      const { currentName } = this;
      const close = parent.accordion && currentName === parent.value;
      const name = close ? '' : currentName;
      parent.switch(name, !this.expanded);
    },
    onTransitionEnd: function onTransitionEnd() {
      if (!this.expanded) {
        this.show = false;
      } else {
        this.$refs.wrapper.style.height = '';
      }
    },
    genTitle: function genTitle() {
      const _this3 = this;

      const h = this.$createElement;
      const { disabled } = this;
      const { expanded } = this;
      const titleSlots = CELL_SLOTS.reduce((slots, name) => {
        if (_this3.slots(name)) {
          slots[name] = function () {
            return _this3.slots(name);
          };
        }

        return slots;
      }, {});

      if (this.slots('value')) {
        titleSlots.default = function () {
          return _this3.slots('value');
        };
      }

      return h(_cell.default, {
        attrs: {
          role: 'button',
          tabindex: disabled ? -1 : 0,
          'aria-expanded': String(expanded),
        },
        class: bem('title', {
          disabled,
          expanded,
        }),
        on: {
          click: this.onClick,
        },
        scopedSlots: titleSlots,
        props: (0, _extends2.default)({}, this.$props),
      });
    },
    genContent: function genContent() {
      const h = this.$createElement;

      if (this.inited) {
        return h('div', {
          directives: [{
            name: 'show',
            value: this.show,
          }],
          ref: 'wrapper',
          class: bem('wrapper'),
          on: {
            transitionend: this.onTransitionEnd,
          },
        }, [h('div', {
          ref: 'content',
          class: bem('content'),
        }, [this.slots()])]);
      }
    },
  },
  render: function render() {
    let _ref;

    const h = arguments[0];
    return h('div', {
      class: [bem(), (_ref = {}, _ref[_constant.BORDER_TOP] = this.index, _ref)],
    }, [this.genTitle(), this.genContent()]);
  },
});

exports.default = _default;
